/******************************************************************************
** @file	WMI_FileDirManager.cpp
** @author  Arturo Aguilar
** @author  Andrea Herbas
**
** @date	Aug 1, 2010
**
** @desc	This is the WMI_FileDirManager class implementation
**			
**
******************************************************************************/

#include "wmi_filedirmanager.h"

using namespace std;

WMI_FileDirManager::WMI_FileDirManager(WMI_Connection* con)
{
	_con = con;
}

WMI_FileDirManager::~WMI_FileDirManager()
{
}

void 
WMI_FileDirManager::getFileDirs(const wstring& path, WMI_Result<WMI_FileDirectory>& fileDirList)
{
	wstring fileQuery;
	wstring dirQuery;

	WMI_Utils::getFileDirQuery(path, fileQuery, dirQuery);

	WMI_HResult* whres = _con->executeQuery(dirQuery.c_str());

    if(!FAILED(whres))
	{
		IEnumWbemClassObject* pEnumeratorD = NULL;
		pEnumeratorD = _con->getIEnumWbemClassObject();
		
		fileDirList.setParent(WMI_Utils::getParent(C_DIR));
		fileDirList.fillVector(pEnumeratorD);
	}

	whres = _con->executeQuery(fileQuery.c_str());

    if(!FAILED(whres))
	{
		IEnumWbemClassObject* pEnumeratorD = NULL;
		pEnumeratorD = _con->getIEnumWbemClassObject();

		fileDirList.fillVector(pEnumeratorD);
	}
}

void 
WMI_FileDirManager::getLogicalDrives(WMI_Result<WMI_Disk>& drivesList)
{
    WMI_HResult* whres = _con->executeQuery(L"SELECT * FROM Win32_LogicalDisk");
	if(!FAILED(whres->getHRESULT()))
	{
	    IEnumWbemClassObject* pEnumerator = NULL;
	    pEnumerator = _con->getIEnumWbemClassObject();

        drivesList.fillVector(pEnumerator);	
	}
    delete whres;
}

void
WMI_FileDirManager::getFileDirName(const wstring& path, wstring& outPath, wstring& outFileName)
{
    int name = path.rfind(L"\\", path.size() - 1);
	outFileName = path.substr(name + 1);
	outPath = path.substr(0, name + 1);
}

void 
WMI_FileDirManager::getFileDirName(const wstring& path, wstring& outFileName)
{
    int name = path.rfind(L"\\", path.size() - 1);
	outFileName = path.substr(name + 1);
}